﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLTV_V2.DataAccess;
using QLTV_V2.BussinessLogic;
using QLTV_V2.BussinessObject;

namespace QLTV_V2
{
    public partial class FrmThanhly : DevComponents.DotNetBar.Office2007RibbonForm
    {
        public FrmThanhly()
        {
            InitializeComponent();
        }
        ConnectData conn = new ConnectData();
        Utilities uti = new Utilities();
        SachBUS sachBUS = new SachBUS();
        PhieuThanhLy phieuTL = new PhieuThanhLy();
        CTThanhLy ct = new CTThanhLy();
        public string manv;
        int kt;
        private void FrmThanhly_Load(object sender, EventArgs e)
        {
            //Load combobox Loai sach
            comb_Loaisach.DataSource = uti.getDataSource("LoaiSach", "TenLoaiSach");
            comb_Loaisach.DisplayMember = "TenLoaiSach";
            comb_Loaisach.ValueMember = "MaLoaiSach";
            //Load combobox phieuTL
            comb_MaPhieuTL.DataSource = uti.getDataSource("PhieuThanhLy", "NgayTL");
            comb_MaPhieuTL.DisplayMember = "TenPhieuTL";
            comb_MaPhieuTL.ValueMember = "MaPhieuTL";
            //Load ngay thang
            date_tungay.Value = System.DateTime.Now;
            date_denngay.Value = System.DateTime.Now;
            date_NgayTL.Value = System.DateTime.Now;
            //lOAD TRẠNG THÁI
            check_Chon.Checked = true;
            comb_MaPhieuTL.Enabled = true;
            check_Ngay.Checked = false;
            btn_TaoMoi.Enabled = true;
            btn_InPhieu.Enabled = true;
            btn_TimKiem.Enabled = true;
        }

        private void btn_TaoMoi_Click(object sender, EventArgs e)
        {
            phieuTL = getTT_PhieuTL();
            if (phieuTL.TenPhieuTL.Equals(""))
            {
                MessageBox.Show("Chưa nhập lý do thanh lý!", "Thông báo");
                txt_TenPhieuTL.Focus();
            }
            else
                if (sachBUS.insertPhieuTL(phieuTL) == true)
                {
                    txt_Mabansach.ReadOnly = false;
                    txt_Mabansach.Text = "";
                    txt_Mabansach.Focus();
                    btn_ThanhLy.Enabled = true;
                    btn_InPhieu.Enabled = true;
                    txt_TenPhieuTL.ReadOnly = true;
                    date_NgayTL.Enabled = false;
                    btn_TimKiem.Enabled = false;
                }
        }
        private void btn_Xoa_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn có muốn xóa " + txt_MaphieuTL.Text + "không?", "Thông báo",
 MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (uti.deleteTable("PhieuThanhLy", "MaPhieuTL", txt_MaphieuTL.Text) == true)
                {
                    int dem = 0;
                    int count = dgv_ThanhLy.Rows.Count - 1;
                    for (int i = 0; i < count; i++)
                    {
                        string mabansach = dgv_ThanhLy.Rows[i].Cells["MaBanSach"].Value.ToString();
                        if (sachBUS.updateBanSach_TL(mabansach, 2) == true)//update trạng thái sách "sẵn sàng"
                            dem++;
                    }

                    if (dem == count)
                    {
                        FrmThanhly_Load(sender, e);
                    }
                }
            }
        }

        private void btn_ThanhLy_Click(object sender, EventArgs e)
        {
            if (uti.CheckExitsValue("BanSach", "MaBanSach", txt_Mabansach.Text) == false)
            {
                if (Check_Exit_DataGrid() == true)
                {
                    ct = getTT_CTThanhLy();
                    if (sachBUS.insertCTThanhLy(ct) == true)
                        if (sachBUS.updateBanSach_TL(txt_Mabansach.Text, 1) == true)//update trạng thái sách --> thanh lý
                            dgv_ThanhLy.DataSource = sachBUS.getCTThanhLy(phieuTL.MaPhieuTL);
                    kt = 1;
                }
                else
                    MessageBox.Show("Mã sách đã được chọn", "Thông báo");
            }
            else
            {
                MessageBox.Show("Mã sách không đúng", "Thông báo");
            }
        }

        bool Check_Exit_DataGrid()
        {
            int n = dgv_ThanhLy.Rows.Count;
            string[] luu = new string[n];
            for (int i = 0; i < n - 1; i++) //Luu vao mang
                luu[i] = dgv_ThanhLy.Rows[i].Cells["MaBanSach"].Value.ToString();
            for (int i = 0; i < n; i++) //Lay ra kiem tra
                if (txt_Mabansach.Text == luu[i])
                    return false;
            return true;
        }
        private void btn_Hoico_Click(object sender, EventArgs e)
        {
            dgv_ThanhLy.CurrentCell = dgv_ThanhLy.Rows[0].Cells[0];
            int dem = 0, kt = 0;
            for (int i = 0; i < dgv_ThanhLy.Rows.Count - 1; i++)
            {

                if (Convert.ToBoolean(dgv_ThanhLy.Rows[i].Cells["chon"].Value) == true)
                {
                    dem++;
                    string mact = dgv_ThanhLy.Rows[i].Cells["MaCTThanhLy"].Value.ToString();
                    string mabansach = dgv_ThanhLy.Rows[i].Cells["MaBanSach"].Value.ToString();
                    if (sachBUS.updateBanSach_TL(mabansach, 2) == true)
                        if (uti.deleteTable("CTPhieuTL", "MaCTThanhLy", mact) == true)
                            kt++;
                }
            }
            if (dem == 0)
                MessageBox.Show("Chưa chọn sách để thanh lý","Thông báo");
            else
                if (dem == kt)
                    dgv_ThanhLy.DataSource = sachBUS.getCTThanhLy(txt_MaphieuTL.Text);
                else
                    MessageBox.Show("Lỗi", "Thông báo");
        }
        
        private void btn_TimKiem_Click(object sender, EventArgs e)
        {
            dgv_ThanhLy.DataSource = conn.GetDataTable(getSQL());
        }
        
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //if (radio_Chon.Checked == true)
            //{
            //    loadStatus(true);
            //    load_changeComboBOX();
            //}
        }

        private void check_Ngay_CheckedChanged(object sender, EventArgs e)
        {
            if (check_Ngay.Checked == true)
            {
                date_tungay.Enabled = true;
                date_denngay.Enabled = true;
            }
            else
            {
                date_tungay.Enabled = false;
                date_denngay.Enabled = false;
            }
        }
        
        private void btn_InPhieu_Click(object sender, EventArgs e)
        {
            frmReportPhieuKK frm = new frmReportPhieuKK();
            frm.maphieuTL = txt_MaphieuTL.Text;
            frm.dk = 2;
            frm.ShowDialog();
        }

        private void dataGridView_Thanhly_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (kt != 1)
            {
                try
                {
                    int dong = e.RowIndex;
                    txt_Mabansach.Text = dgv_ThanhLy.Rows[dong].Cells["MaBanSach"].Value.ToString();
                }
                catch { }
            }
        }
        #region Lấy thông tin
        string getSQL()
        {
            int type = uti.GetTypeDate();
            string sql = "select MaCTThanhLy,a.MaPhieuTL,a.MaBanSach,TenDauSach, TinhTrang, TrangThai " +
                            "from PhieuThanhLy d, CTPhieuTL a, BanSach b, DauSach c " +
                            "where d.MaPhieuTL=a.MaPhieuTL and a.MaBanSach=b.MaBanSach and b.MaDauSach=c.MaDauSach ";
            if (txt_MaCB.Text != "")
                sql += "and a.MaBanSach like N'%" + txt_MaCB.Text + "%' ";
            if (txt_Nhande.Text != "")
                sql += "and c.TenDauSach like N'%" + txt_Nhande.Text + "%' ";
            if (comb_Loaisach.SelectedIndex != 0)
                sql += "and c.MaLoaiSach=N'"+comb_Loaisach.SelectedValue.ToString()+"' ";
            if (check_Ngay.Checked == true)
                sql += "and NgayTL >='"+date_tungay.Value.ToString("yyyy/MM/dd")+"' and NgayTL<='"+date_denngay.Value.ToString("yyyy/MM/dd")+"' ";
            return sql;
        }
        PhieuThanhLy getTT_PhieuTL()
        {
            phieuTL.MaPhieuTL = txt_MaphieuTL.Text;
            phieuTL.TenPhieuTL = txt_TenPhieuTL.Text;
            phieuTL.NgayTL = date_NgayTL.Value;
            phieuTL.MaNV = manv;
            return phieuTL;
        }
        CTThanhLy getTT_CTThanhLy()
        {
            string lastID = uti.GetLastID_DK("CTPhieuTL", "MaPhieuTL", txt_MaphieuTL.Text, "MaCTThanhLy", "MaCTThanhLy", txt_MaphieuTL.Text + "-0000");
            string nextID = uti.GetNextID(lastID, txt_MaphieuTL.Text + "-");
            ct.MaCTThanhLy = nextID;
            ct.MaPhieuTL = txt_MaphieuTL.Text;
            ct.MaBanSach = txt_Mabansach.Text;
            return ct;
        }
        void load_changeComboBOX()
        {
            try
            {
                phieuTL = sachBUS.getPhieuTL_object(comb_MaPhieuTL.SelectedValue.ToString());
                txt_MaphieuTL.Text = phieuTL.MaPhieuTL;
                txt_TenPhieuTL.Text = phieuTL.TenPhieuTL;
                date_NgayTL.Value = phieuTL.NgayTL;
                dgv_ThanhLy.DataSource = sachBUS.getCTThanhLy(txt_MaphieuTL.Text);
            }
            catch
            {
               // MessageBox.Show("Không có dữ liệu", "Thông báo");
            }
        }
        void loadStatus(bool e)
        {
            //radio_Chon.Checked = e;
            txt_MaphieuTL.ReadOnly = !e;
            txt_TenPhieuTL.ReadOnly = e;
            date_NgayTL.Enabled = !e;
        }
        #endregion

        private void dgv_ThanhLy_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            uti.dgv_GetSTT_RowHeader(sender, e, dgv_ThanhLy);
        }

        private void check_Chon_CheckedChanged(object sender, EventArgs e)
        {
            //Load combobox phieuTL
            comb_MaPhieuTL.DataSource = uti.getDataSource("PhieuThanhLy", "NgayTL");
            comb_MaPhieuTL.DisplayMember = "TenPhieuTL";
            comb_MaPhieuTL.ValueMember = "MaPhieuTL";
            
            if (check_Chon.Checked == true)
            {
                loadStatus(false);
                load_changeComboBOX();
                btn_InPhieu.Enabled = true;
                btn_TaoMoi.Enabled = false;
                comb_MaPhieuTL.Enabled = true;
                txt_Mabansach.ReadOnly = false;
                btn_ThanhLy.Enabled = true;
                check_TaoMoi.Checked = false;
                txt_TenPhieuTL.ReadOnly = true;
                txt_Mabansach.ReadOnly = true;
                btn_TimKiem.Enabled = false;
               if(comb_MaPhieuTL.SelectedIndex==0)
                   load_changeComboBOX();
            }
            else
            {
                comb_MaPhieuTL.Enabled = false;
                btn_TaoMoi.Enabled = true;
                btn_InPhieu.Enabled = false;
                check_TaoMoi.Checked = true;
                btn_ThanhLy.Enabled = false;
                txt_TenPhieuTL.ReadOnly = false;
            }
        }

        private void check_TaoMoi_CheckedChanged(object sender, EventArgs e)
        {
            if (check_TaoMoi.Checked == true)
            {
                btn_TaoMoi.Enabled = true;
                btn_ThanhLy.Enabled = false;
                check_Chon.Checked = false;
                //Load trạng thái
                loadStatus(false);
                string lastID = uti.GetLastID("PhieuThanhLy", "MaPhieuTL", "MaPhieuTL", "TL-0000");
                string nextID = uti.GetNextID(lastID, "TL-");
                txt_MaphieuTL.Text = nextID;
                txt_TenPhieuTL.Text = "";
                txt_TenPhieuTL.ReadOnly = false;
                date_NgayTL.Value = System.DateTime.Now;
                date_NgayTL.Enabled = true;
                txt_MaCB.Text = "";
                dgv_ThanhLy.DataSource = sachBUS.getCTThanhLy(txt_MaphieuTL.Text);
                txt_Mabansach.ReadOnly = true;
                btn_TimKiem.Enabled = false;
            }
            else
            {
                check_Chon.Checked = true;
                btn_TaoMoi.Enabled = false;
                txt_TenPhieuTL.ReadOnly = true;
            }
        }

        private void comb_MaPhieuTL_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(check_Chon.Checked==true)
                load_changeComboBOX();
            
        }

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            FrmThanhly_Load(sender, e);
        }

    }
}
